package com.example.smartcitymovie.ui.movie;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.BannerMovieTimesAdapter;
import com.example.smartcitymovie.bean.MovieMessageBean;
import com.example.smartcitymovie.bean.TheaterTimesListBean;
import com.example.smartcitymovie.bean.TheatreMessageBean;
import com.example.smartcitymovie.dataRepo.ApiData;
import com.example.smartcitymovie.databinding.ActivityChooseTharterTimesBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChooseTharterTimes extends AppCompatActivity {

    ActivityChooseTharterTimesBinding binding;
    private int movieId;
    private int tharterId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChooseTharterTimesBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        movieId = getIntent().getIntExtra("id", 0);
        if (movieId != 0) {
            getSupportActionBar().setTitle("选择场次");
        }else{
            getSupportActionBar().setTitle("影片详情");
        }
        tharterId = getIntent().getIntExtra("tharterId", 0);
        intiView();
    }

    private void intiView() {

        //加载影院基本信息
        TheatreMessageBean theatreMessageBean = ApiData.getTheaterTimesListBeanNyThreadId(tharterId);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                binding.TVTharterAddress.setText(theatreMessageBean.getData().getAddress());
                binding.TVTharterName.setText(theatreMessageBean.getData().getName());
            }
        });
        //加载影片基本信息
        TheaterTimesListBean timesListBean = ApiData.getTheaterTimesListBeanByMovieId(movieId);
        List<MovieMessageBean.DataBean> movieMessageBeanList = new ArrayList<>();
        //所有场次的播放日期
        HashMap<String, List<TheaterTimesListBean.RowsBean>> timeMessage = new HashMap<>();
        for (TheaterTimesListBean.RowsBean bean : timesListBean.getRows()
        ) {
            MovieMessageBean detailDataById = ApiData.getMovieDetailDataById(bean.getMovieId());
            movieMessageBeanList.add(detailDataById.getData());

            if (!timeMessage.containsKey(bean.getPlayDate())) {
                timeMessage.put(bean.getPlayDate(), new ArrayList<>());
                timeMessage.get(bean.getPlayDate()).add(bean);
            } else {
                if (!timeMessage.containsValue(bean)) {
                    timeMessage.get(bean.getPlayDate()).add(bean);
                }
            }
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //加载饮片信息的轮播图
                BannerMovieTimesAdapter bannerMovieTimesAdapter = new BannerMovieTimesAdapter(movieMessageBeanList);
                binding.TimesMovieMessage.setAdapter(bannerMovieTimesAdapter);
                List<Fragment> fragments = new ArrayList<>();
                for (int i = 0; i < timeMessage.keySet().size(); i++) {
                    List<TheaterTimesListBean.RowsBean> rowsBeans = timeMessage.get(timeMessage.keySet().toArray()[i]);
                    fragments.add(new MovieTimesFragment(rowsBeans));
                }
                binding.TVPFShowTimes.setAdapter(getSupportFragmentManager(), getLifecycle(), fragments, new ArrayList<>(timeMessage.keySet()));
            }
        });
    }
}